﻿using System;
using System.Web.Mvc;
using System.Web;

namespace mForum.Web.Common.Infrastructure
{
    public class SessionManager : ISessionManager
    {
        private IHttpContextProvider _httpContextProvider;

        public SessionManager(IHttpContextProvider provider)
        {
            _httpContextProvider = provider;
        }

        public void Set<T>(string name, T value)
        {
            _httpContextProvider.Context.Session.Add(name, value);
        }

        public T Get<T>(string name)
        {
            return (T)_httpContextProvider.Context.Session[name];
        }

        public T TryGet<T>(string name)
        {
            try
            {
                return (T)_httpContextProvider.Context.Session[name];
            }
            catch (NullReferenceException e)
            {
                return default(T);
            }
        }

        public void Abandon()
        {
            _httpContextProvider.Context.Session.Abandon();
        }
    }
}
